package co.unal.visor.model;

import co.unal.visor.util.BitWise;
import co.unal.visor.util.Pixel;
import co.unal.visor.util.RGB;

public class Image {

	private long headerIndex;
	private int bitspp;
	private int width;
	private int height;
	private byte[][] stripArray;
	private boolean littleEndian = true;
	private byte[] vector;
	private Pixel[][] pixelMatrix;
	private long[] pixelHistograma = new long[256];

	/**
	 * Funcion que toma el stripArray de la misma clase y retorna su
	 * representacion como un arreglo matricial de objetos Pixel
	 * 
	 * @return
	 */
	public Pixel[][] createPixelMatrix() {
		Pixel[][] retorno = new Pixel[height][width];

		// littleendian
		if (littleEndian) {
			int k = 0;
			int j = 0;
			for (int i = 0; i < stripArray.length; i++) {
				byte[] currentStrip = stripArray[i];
				int tempIndex = 0;
				System.out.println("createPixelMatrix() " + i);
				while (k < height) {
					for (j = 0; j < width; j++) {
						if (tempIndex < stripArray[i].length) {
							/*
							 * Los tres canales tienen la misma intensidad en el
							 * blanco y negro por eso se toma cierto porcentajes
							 * existen otros metodos que sacan el promedio de
							 * los tres los suman y ese es el tono que se deja.
							 * pero no es muy acertado en matlab la formula es
							 * red*0,2989+green*0,587+ blue*0,1140
							 */

							int hexavalue = BitWise
									.byteArrayToIntLE(new byte[] {

											// (byte) medRGB, (byte) medRGB,
											// (byte) medRGB });

											(currentStrip[tempIndex + 0]),
											(currentStrip[tempIndex + 1]),
											(currentStrip[tempIndex + 2]) });

							retorno[k][j] = new RGB(hexavalue);
							tempIndex += (bitspp / 8);
						}
					}
					k++;
				}
			}

		} else {
			int k = 0;
			int j = 0;

			for (int i = 0; i < stripArray.length; i++) {
				byte[] currentStrip = stripArray[i];
				// System.out.println("bytes por strip() " +
				// currentStrip.length);
				int tempIndex = 0;
				// System.out.println("createPixelMatrix() strip:" + i);
				while (tempIndex < stripArray[i].length) {
					// System.out.println("fila " + k);
					for (j = 0; j < width; j++) {
						// if (tempIndex < stripArray[i].length) {
						// System.out.println("Columna " + j);
						int hexavalue = BitWise.byteArrayToIntLE(new byte[] {
								(currentStrip[tempIndex + 2]),
								(currentStrip[tempIndex + 1]),
								(currentStrip[tempIndex + 0]) });
						retorno[k][j] = new RGB(hexavalue);
						tempIndex += (bitspp / 8);
						// }else{
						// System.out.println("tempIndex fuera de rango "+tempIndex);
						// break;
						// }
					}
					k++;
				}
			}
		}
		return retorno;
	}

	public Image() {

	}

	public long getHeaderIndex() {
		return headerIndex;
	}

	public void setHeaderIndex(long headerIndex) {
		this.headerIndex = headerIndex;
	}

	public int getBitspp() {
		return bitspp;
	}

	public void setBitspp(int bitspp) {
		this.bitspp = bitspp;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void setStripArray(byte[][] stripArray) {
		this.stripArray = stripArray;
	}

	public byte[][] getStripArray() {
		return stripArray;
	}

	public void setStripArrayAt(byte[] row, int i) {

		if (stripArray == null) {
			stripArray = new byte[1][];
		}
		if (stripArray[i] != null) {
			System.out.println("**");
			stripArray[i] = row;
		} else {
			System.out.println("__");
			stripArray[0] = row;
		}

	}

	public boolean isLittleEndian() {
		return littleEndian;
	}

	public void setLittleEndian(boolean littleEndian) {
		this.littleEndian = littleEndian;
	}

	public void setVector(byte[] vector) {
		this.vector = vector;
	}

	public byte[] getVector() {
		return vector;
	}

	public Pixel[][] getPixelMatrix() {
		return pixelMatrix;
	}

	public void setPixelMatrix(Pixel[][] pixelMatrix) {
		this.pixelMatrix = pixelMatrix;
	}

	

	public long[] getPixelHistograma() {
		long[] intArray = new long[256];
		System.out.print("x: " + width + " ;y: " + height + "arreglolength: "
				+ intArray.length);
		for (int x = 0; x < intArray.length; x++) {
			intArray[x] = 0;
		}

		int k = 0;
		int j = 0;
		for (int i = 0; i < stripArray.length; i++) {
			byte[] currentStrip = stripArray[i];
			int tempIndex = 0;
			System.out.println("createPixelMatrix() " + i);
			while (k < height) {
				for (j = 0; j < width; j++) {
					if (tempIndex < stripArray[i].length) {

						// double medRGB = ((currentStrip[tempIndex + 0] *
						// 0.2989
						// + currentStrip[tempIndex + 1] * .5870 +
						// currentStrip[tempIndex + 2] * 0.1140));
						int medRGB = 128+((currentStrip[tempIndex + 0]));
						// System.out.println(medRGB);
						intArray[medRGB] = intArray[medRGB] + 1;

						tempIndex += (bitspp / 8);
					}
				}
				k++;
			}
			stripArray[i] = currentStrip;
		}

		return intArray;
	}

	public void setPixelHistograma(long[] pixelHistograma) {
		this.pixelHistograma = pixelHistograma;
	}

}
